﻿@using XStudio.XCRF.Web.Models.Hospital;
@{
    ViewBag.Title = "HospitalList";
    Layout = "~/Views/Shared/Master.cshtml";
}

<h2>医院列表</h2>
@(Html.Telerik().Grid<HospitalListModel>()
    .Name("HospitalGrid")
    .ToolBar(config =>
    {
        if(LoginSession.IsAdministrator)
            config.Custom().Name("Add").Text("添加新医院").Action<HospitalController>(c => c.HospitalAdd());
    })
    .DataKeys(keys => keys.Add(hospital => hospital.ID))
    .Columns(columns =>
    {
        columns.Bound(hospital => hospital.ID).Hidden();
        columns.Bound(hospital => hospital.Code).Filterable(true);
        columns.Bound(hospital => hospital.Name).Filterable(true);
        columns.Bound(hospital => hospital.Owner.RealName).ReadOnly().Filterable(true);
        columns.Bound(hospital => hospital.CRFCount).ClientTemplate(
            "<a href='javascript:CRFNumbers(\"<#= ID #>\");'><#= CRFCount #></a>"
        ).Title("病例数").Filterable(true) ;
        columns.Command(commands =>
        {
            commands.Custom("Detail").Text("查看详情").Ajax(true);
            commands.Custom("Manage").Text("人员情况").Ajax(true);
            if (LoginSession.IsAdministrator)
                commands.Delete();

        })
        .Title("操作");
    })
    .ClientEvents(events =>
    {
        events.OnCommand("OnCommand");
    })
    .DataBinding(bind =>
    {
        bind.Ajax().Select<HospitalController>(c => c._HospitalList())
            .Delete<HospitalController>(c => c._HospitalDelete(null));
    })
    .Pageable(pager => pager.PageSize(10).Style(GridPagerStyles.NextPreviousAndNumeric))
    .Filterable(filtering => filtering.Enabled(true))
    .Editable(edit => edit.Mode(GridEditMode.InLine))
    .Sortable(sort=>sort.SortMode(GridSortMode.MultipleColumn).OrderBy(ord=>ord.Add(o=>o.CRFCount)))
)
<script type="text/javascript">
    function Detail(id) {
        var url = "@(Url.Action<HospitalController>(c => c.HospitalDetail(Guid.Empty)))";
        url = url.replace("@Guid.Empty", id);
        window.location = url;
    }
    function CRFNumbers(id) {
        var url = "@(Url.Action<CRFController>(c => c.CRFList(Guid.Empty)))";
        url = url.replace("@Guid.Empty", id);
        window.location = url;
    }
    function Manage(id){
        var url = "@(Url.Action<HospitalController>(c => c.HospitalMemberManage(Guid.Empty)))";
        url = url.replace("@Guid.Empty", id);
        window.location = url;
    }
    function OnCommand(e) {
        switch (e.name) {
            case "Detail": Detail(e.dataItem.ID); break;
            case "Manage": Manage(e.dataItem.ID); break;
        }
    }
</script>